package com.shuoyue.ycgk.ui.mine.coupon;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.Coupon;
import com.shuoyue.ycgk.entity.Rule;
import com.shuoyue.ycgk.ui.common.RuleContract;
import com.shuoyue.ycgk.ui.course.CourseTypeActivity;
import com.shuoyue.ycgk.ui.mine.vip.MyVipActivity;
import com.shuoyue.ycgk.ui.papergroups.groupcommon.PaperGroupCommonActivity;
import com.shuoyue.ycgk.ui.shop.ShopTabActivity;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;
import com.shuoyue.ycgk.views.dialog.common.BigHintDialog;
import com.shuoyue.ycgk.views.dialog.common.HintDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的优惠券
 * Forty'7
 */
public class CouponVilidateActivity extends BaseMvpActivity<CouponContract.Presenter> implements CouponContract.View, RuleContract.View {
    public static void start(Context context) {
        context.startActivity(new Intent(context, CouponVilidateActivity.class)
        );
    }

    @BindView(R.id.buy)
    TextView buy;
    @BindView(R.id.lay_invilidatecoupon)
    LinearLayout layInvilidatecoupon;


    //    ListWithPage listPageBean;
    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.lay_coupon)
    LinearLayout layCoupon;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    CouponAdapter couponAdapter;
    RuleContract.Presenter rulePresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_coupon;
    }

    @Override
    protected void initView() {
        pageTitle.setText("优惠券");
        layInvilidatecoupon.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 4), false));
        couponAdapter = new CouponAdapter(null, 2);
        recyclerView.setAdapter(couponAdapter);
        registEmptyView(couponAdapter);
        couponAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            Coupon coupon = (Coupon) adapter.getItem(position);
            switch (view.getId()) {
                case R.id.tv_explain:
                    HintDialog hintDialog = new HintDialog(mContext,
                            "使用说明", coupon.getUseExplain(), Gravity.LEFT);
                    hintDialog.show();
                    break;
                case R.id.lay_use:
                    String typeName = "";
                    switch (coupon.getUseType()) {

                        case "":
                            //空去提示下就可以了
                            toast("通用优惠券，可以全场通用");
                            break;
                        case "1":
                            //课程
                            if (TextUtils.isEmpty(coupon.getSubjectTypeId())) {
                                toast("适用所有课程，请前往课程使用");
                                return;
                            }
                            if (coupon.getSubjectTypeId().equals("40")) {
                                typeName = "事业单位";
                            }
                            if (coupon.getSubjectTypeId().equals("39")) {
                                typeName = "公务员";
                            }
                            if (coupon.getSubjectTypeId().equals("41")) {
                                typeName = "教师招聘";
                            }
                            startActivity(new Intent(mContext, CourseTypeActivity.class)
                                    .putExtra("typeName", typeName));
                            break;
                        case "2":
                            // 图书
                            if (TextUtils.isEmpty(coupon.getSubjectTypeId())) {
                                toast("适用所有题库，请前往题库使用");
                                return;
                            }
                            if (coupon.getSubjectTypeId().equals("39")) {
                                typeName = "事业单位";
                            }
                            if (coupon.getSubjectTypeId().equals("40")) {
                                typeName = "公务员";
                            }
                            if (coupon.getSubjectTypeId().equals("41")) {
                                typeName = "教师招聘";
                            }

                            startActivity(new Intent(mContext, ShopTabActivity.class)
                                    .putExtra("typeName", typeName));
                            break;
                        case "4":
                            // 考前冲刺
                            toast("适用考前冲刺，请前往考前冲刺使用");
                            break;
                        case "5":
                            // VIP
                            startActivity(new Intent(mContext, MyVipActivity.class));

                            break;
                        case "6":
                            // 习题包
                            if (TextUtils.isEmpty(coupon.getSubjectTypeId())) {
                                toast("适用所有题库，请前往题库使用");
                                return;
                            }
                            if (coupon.getSubjectTypeId().equals("39")) {
                                typeName = "事业单位";
                            }
                            if (coupon.getSubjectTypeId().equals("40")) {
                                typeName = "公务员";
                            }
                            if (coupon.getSubjectTypeId().equals("41")) {
                                typeName = "教师招聘";
                            }
                            PaperGroupCommonActivity.start(this, -1, 39, false, typeName);

                            break;
                    }
                    break;
            }
        });
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getCoupons(1, 1);
            refreshLayout.finishRefresh();
        });

        View footerView = LayoutInflater.from(mContext).inflate(R.layout.lay_vilidatecoupon, null);
        footerView.setOnClickListener(v -> startActivity(new Intent(mContext, CouponInvilidateActivity.class)));
        couponAdapter.addFooterView(footerView);
        buy.setVisibility(View.VISIBLE);
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter = new CouponContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getCoupons(1, 1);
        rulePresenter = new RuleContract.Presenter();
        rulePresenter.attachView(this);
    }


    @OnClick({R.id.back, R.id.tv_right, R.id.buy, R.id.lay_invilidatecoupon, R.id.lay_coupon})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.lay_invilidatecoupon:
                startActivity(new Intent(mContext, CouponInvilidateActivity.class));
                break;
            case R.id.buy:
                startActivity(new Intent(mContext, CouponUsedActivity.class));
                break;
            case R.id.lay_coupon:
                rulePresenter.getRule(5);
                break;
        }
    }

    @Override
    public void getCouponList(List<Coupon> list) {
        if (list != null && list.size() > 0) {
            layInvilidatecoupon.setVisibility(View.GONE);
        } else {
            layInvilidatecoupon.setVisibility(View.VISIBLE);
        }
        couponAdapter.resetData(list);
    }

    @Override
    public void setRule(Rule userInfo) {
        BigHintDialog hintDialog = new BigHintDialog(mContext, "如何获取更多优惠券",
                userInfo.getContent(), Gravity.LEFT);
        hintDialog.show();
    }
}
